package com.sailfish.springbootdemo.dao.db5;

import com.sailfish.springbootdemo.pojo.db5.BoardProportion;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface BoardProportionDao {

    @Results(id = "boardProportionResultMap", value = {
            @Result(column = "NH3C_PCBREVISION", property = "nh3cPcbrevision", jdbcType = JdbcType.VARCHAR),
            @Result(column = "NH3C_PRINTBOCODING", property = "nh3cPrintbocoding", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CURRENT_STATUS", property = "currentStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "NH3C_ISCHOICE", property = "nh3cIschoice", jdbcType = JdbcType.VARCHAR),
            @Result(column = "item_id", property = "itemId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "NH3C_FOREIGNASSMAN", property = "nh3cForeignassman", jdbcType = JdbcType.VARCHAR),
            @Result(column = "NH3C_FACTORYCODE", property = "nh3cFactorycode", jdbcType = JdbcType.VARCHAR),
//            @Result(column = "APPLICATION_TIME", property = "applicationTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "nh3c_BoTime", property = "nh3cBoTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "NH3C_LayersNo", property = "nh3cLayersNo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "NH3C_BoardName", property = "nh3cBoardName", jdbcType = JdbcType.VARCHAR),
    })

    @Select("<script> " +
            "select t3.*,t4.NH3C_LayersNo,t4.NH3C_BoardName from " +
            "(select t1.NH3C_PCBREVISION,t1.NH3C_PRINTBOCODING,t1.CURRENT_STATUS,t1.nh3c_BoTime," +
            "t2.NH3C_ISCHOICE,t2.item_id,t2.NH3C_FOREIGNASSMAN,t2.NH3C_FACTORYCODE " +
            "from BPM_ISSUE_H3C08N082_MAIN t1 join IBPM_h3c08n082_factory t2 " +
            "on t1.item_id=t2.ITEM_ID " +
            "<where>" +
            "and trim(t2.NH3C_ISCHOICE) ='true' " +
            "<if test='startTime != null and endTime != null '>" +
            "and nh3c_BoTime between #{startTime} and #{endTime}  " +
            "</if>" +
//            "<if test='startTime != null and endTime == null '>" +
//            "and nh3c_BoTime > #{startTime} "+
//            "</if>" +
//            "<if test='startTime == null and endTime != null '>" +
//            "and nh3c_BoTime < #{endTime} "+
//            "</if>" +
            "<if test='factorys != null '>" +
            "and trim(t2.NH3C_FOREIGNASSMAN) in " +
            "<foreach item=\"item\" collection=\"factorys\" open=\"(\" separator=\",\" close=\")\">\n" +
            " #{item}\n" +
            "</foreach>" +
            "</if>" +
            "</where>" +
            ") t3 left join BPM_ISSUE_H3C08N082_CASTPLATEINFORMATION t4 " +
            "on t3.NH3C_PRINTBOCODING=t4.NH3C_PartNo " +
            "and t3.NH3C_PCBREVISION = t4.NH3C_RevisionNo" +
            " order by create_time desc" +
            "<if test='begin != null and pageSize != null and pageSize != \"\" '>" +
            " limit #{begin}, #{pageSize}" +
            "</if>" +
            "</script>")
    List<BoardProportion> getList(@Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("factorys") List<String> factorys,@Param("begin") Integer begin, @Param("pageSize") Integer pageSize);


    @Select("<script>" +
            "select nh3c_BoTime from BPM_ISSUE_H3C08N082_MAIN " +
            "<where>" +
            "<if test='productCode0301 != null '>" +
            "and NH3C_PRINTBOCODING = #{productCode0301}  " +
            "</if>" +
            "<if test='boardVer != null '>" +
            "and NH3C_PCBREVISION = #{boardVer}  " +
            "</if>" +
            "</where>" +
            "</script>")
    @ResultMap(value = "boardProportionResultMap")
    BoardProportion getByNameAndVer(@Param("productCode0301") String productCode0301, @Param("boardVer") String boardVer);

    @Select("<script>" +
            "select NH3C_PRINTBOCODING,NH3C_PCBREVISION,nh3c_BoTime from BPM_ISSUE_H3C08N082_MAIN " +
            "<where>" +
            "<if test='startTime != null and endTime != null '>" +
            "and nh3c_BoTime between #{startTime} and #{endTime}  " +
            "</if>" +
            "<if test='code0301 != null '>" +
            "and NH3C_PRINTBOCODING like CONCAT('%', #{code0301},'%') "+
            "</if>" +
            "<if test='ver0301 != null'>" +
            "and NH3C_PCBREVISION like CONCAT('%',#{ver0301},'%') "+
            "</if>" +
            "</where>" +
            "order by nh3c_BoTime desc " +
            "<if test='begin != null and pageSize != null and pageSize != \"\" '>" +
            " limit #{begin}, #{pageSize}" +
            "</if>" +
            "</script>")
    @ResultMap(value = "boardProportionResultMap")
    List<BoardProportion> getByTime(@Param("startTime") Date startTime, @Param("endTime") Date endTime,
                                    @Param("code0301") String code0301,@Param("ver0301") String ver0301,@Param("begin") Integer begin, @Param("pageSize") Integer pageSize);

    @Select("<script>" +
            "select NH3C_PRINTBOCODING,NH3C_PCBREVISION,nh3c_BoTime from BPM_ISSUE_H3C08N082_MAIN " +
            "<where>" +
            "<if test='startTime != null and endTime != null '>" +
            "and nh3c_BoTime between #{startTime} and #{endTime}  " +
            "</if>" +
            "<if test='code0301 != null '>" +
            "and NH3C_PRINTBOCODING like CONCAT('%', #{code0301},'%') "+
            "</if>" +
            "<if test='ver0301 != null'>" +
            "and NH3C_PCBREVISION like CONCAT('%',#{ver0301},'%') "+
            "</if>" +
            "</where>" +
            "order by nh3c_BoTime desc " +
            "</script>")
    @ResultMap(value = "boardProportionResultMap")
    List<BoardProportion> getByTimeAll(@Param("startTime") Date startTime, @Param("endTime") Date endTime,
                                    @Param("code0301") String code0301,@Param("ver0301") String ver0301);

}
